package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.mapper.StrategyCatalogMapper;
import cn.wolfcode.trip.base.mapper.StrategyDetailMapper;
import cn.wolfcode.trip.base.query.StrategyCatalogQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {
    @Autowired
    private StrategyCatalogMapper catalogMapper;

    public PageInfo<?> query(StrategyCatalogQueryObject qo) {
        qo.setOrderBy("sc.sequence");
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<StrategyCatalog> list = catalogMapper.queryForList(qo);
        return new PageInfo<>(list);
    }

    public void saveOrUpdate(StrategyCatalog catalog) {
        if (catalog.getSequence() == null){
            int sequence = catalogMapper.selectSequenceByStrategyId(catalog.getStrategy().getId());
            if (sequence == 0){
                catalog.setSequence(sequence);
            }else {
                catalog.setSequence(sequence+1);
            }
        }
        if (catalog.getId() == null){
            catalogMapper.insert(catalog);
        }else {
            catalogMapper.updateByPrimaryKey(catalog);
        }
    }

    public List<StrategyCatalog> getCatalogById(Long id) {
        List<StrategyCatalog> catalogs = catalogMapper.selectCatalogById(id);
        return catalogs;
    }


}
